package casa.depesos.app.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.common.base.task.SilentTask;
import com.common.base.task.TaskEngine;

import casa.depesos.app.com.R;
import casa.depesos.app.com.adapter.MeAdapter;
import casa.depesos.app.com.databinding.ActivityMainBinding;
import casa.depesos.app.com.dlg.BaseDialogFragment;
import casa.depesos.app.com.dlg.MsgDlg;
import casa.depesos.app.com.frag.CheckingFragment;
import casa.depesos.app.com.frag.GiveFailFragment;
import casa.depesos.app.com.frag.HomeFragment;
import casa.depesos.app.com.frag.NetFailFragment;
import casa.depesos.app.com.frag.RefuseFragment;
import casa.depesos.app.com.frag.RepayFragment;
import casa.depesos.app.com.frag.TakeCheckFragment;
import casa.depesos.app.com.frag.TakeCheckOkFragment;
import casa.depesos.app.com.frag.TakeFailFragment;
import casa.depesos.app.com.frag.TakeFragment;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.BannerBean;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.bean.ProgressBean;
import casa.depesos.app.com.http.req.BannerReq;
import casa.depesos.app.com.http.req.IsFirstReq;
import casa.depesos.app.com.http.req.ProgressReq;
import casa.depesos.app.com.http.req.no.HasMemberReq;
import casa.depesos.app.com.it.Refreshable;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Update;
import casa.depesos.app.com.utils.Utils;

public class MainActivity extends BaseActivity {

    ActivityMainBinding binding;
    int lastStatus = -1;
    Fragment fragment;

    @Override
    protected boolean needPadding() {
        return false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        binding.navView.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        binding.list.setLayoutManager(new LinearLayoutManager(this));
        binding.list.setAdapter(new MeAdapter(this));
        binding.getRoot().addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                new HasMemberReq().req();
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    public int getLastStatus() {
        return lastStatus;
    }

    public void openDrawer() {
        binding.getRoot().open();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    public void getData() {
        getData(false);
    }

    private void getData(boolean refresh) {
        if (!refresh) {
            showLoading();
        }
        new ProgressReq().req(new ReqCallback<ProgressBean>() {
            @Override
            public void callback(boolean success, ProgressBean bean) {
                hideLoading();
                if (refresh) {
                    binding.refresh.setRefreshing(false);
                }
                if (success && bean.isValid()) {
                    buildChild(bean.data.status, refresh);
                } else if (Constant.white) {
                    buildChild(Utils.getStatus(), false);
                } else {
                    lastStatus = -1;
                    replaceFragment(R.id.container, new NetFailFragment());
                }
            }
        });
    }

    public void netFail() {
        lastStatus = -1;
        replaceFragment(R.id.container, new NetFailFragment());
    }

    private void buildChild(int status, boolean refresh) {
        try {
            if (lastStatus == status) {
                if ((Constant.reset || refresh) && fragment instanceof Refreshable) {
                    ((Refreshable) fragment).refresh();
                }
                return;
            }
            lastStatus = status;
            if (status == 600) {
                Report.toMone();
            }
            switch (status) {
                case 0:
                case 10:
                case 20:
                case 25:
                case 30:
                case 40:
                case 50:
                case 600:
                    fragment = new HomeFragment().setStatus(status);
                    break;
                case 61:
                    showLoading();
                    TaskEngine.getInstance().submit(new SilentTask("") {
                        @Override
                        public void runInBackground() throws Exception {
                            GetCodeBean bean = new IsFirstReq().req();
                            h.post(new Runnable() {
                                @Override
                                public void run() {
                                    hideLoading();
                                    fragment = new CheckingFragment().setStatus(bean != null && "true".equalsIgnoreCase(bean.toString()));
                                    replaceFragment(R.id.container, fragment);
                                }
                            });
                        }
                    });
                    return;
                case 62:
                    fragment = new RefuseFragment();
                    break;
                case 60:
                    fragment = new TakeFragment();
                    break;
                case 91:
                    fragment = new TakeCheckFragment();
                    break;
                case 92:
                    fragment = new TakeFailFragment();
                    break;
                case 90:
                case 300:
                    fragment = new TakeCheckOkFragment();
                    break;
                case 301:
                case 302:
                    fragment = new GiveFailFragment().setParam(status == 301);
                    break;
                case 400:
                case 500:
                    fragment = new RepayFragment().setStatus(status);
                    break;
            }
            if (fragment != null) {
                replaceFragment(R.id.container, fragment);
                getAd();
            }
        } finally {
            Constant.reset = false;
            new Update().check(this, false, false);
        }
    }

    public void showHome(int from) {
        if (from == 0) {
            fragment = new HomeFragment().setStatus(62);
            replaceFragment(R.id.container, fragment);
        } else if (from == 1) {
            fragment = new HomeFragment().setStatus(92);
            replaceFragment(R.id.container, fragment);
        }
    }

    public void setAvatar(String name, boolean isFemale) {
        binding.name.setText(name);
        if (isFemale) {
            binding.name.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.woman, 0, 0, 0);
        } else {
            binding.name.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.avatar, 0, 0, 0);
        }
    }


    public void jump(int p, String title) {
        Intent i = null;
        if (Constant.white) {
            if (p == 0) {
                p = 3;
            } else if (p == 1) {
                p = 5;
            } else if (p == 2) {
                p = 6;
            } else if (p == 3) {
                p = 7;
            }
        }
        switch (p) {
            case 0:
                i = new Intent(this, OrderActivity.class);
                break;
            case 1:
                i = new Intent(this, BankCardActivity.class);
                i.putExtra("status", getLastStatus());
                break;
            case 2:
                i = new Intent(this, MeActivity.class);
                break;
            case 3:
                i = new Intent(this, AccountActivity.class);
                break;
            case 4:
                Utils.toMsg();
                return;
            case 5: {
                i = new Intent(this, WebActivity.class);
                i.putExtra("url", Constant.PRIVACY);
                i.putExtra("title", title);
                break;
            }
            case 6:
                i = new Intent(this, AboutActivity.class);
                break;
            case 7: {
                i = new Intent(this, WebActivity.class);
                i.putExtra("url", Constant.HELP);
                i.putExtra("title", title);
                break;
            }
        }
        if (i != null) {
            startActivity(i);
        }
    }

    MsgDlg dlg = null;

    protected void getAd() {
        if (dlg != null) {
            return;
        }
        new BannerReq("6").req(new ReqCallback<BannerBean>() {
            @Override
            public void callback(boolean success, BannerBean bean) {
                if (success && bean.isValid()) {
                    Report.show("ACTION_UU_002");
                    dlg = new MsgDlg().setData(bean.list).setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                        @Override
                        public void onDismiss(boolean isOk, Object value) {
                            dlg = null;
                        }
                    });
                    dlg.show(getSupportFragmentManager(), "");
                }
            }
        });
    }
}
